package com.springboot.middleware.middleware_rabbitmq_logforlogin.server.consumer;

import com.springboot.middleware.middleware_rabbitmq_logforlogin.server.dto.UserLoginDto;
import com.springboot.middleware.middleware_rabbitmq_logforlogin.server.service.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
* 系统日志记录-消费者
* */
@Component
public class LogConsumer {
    private static final Logger log = LoggerFactory.getLogger(LogConsumer.class);
    //定义系统日志服务实例
    @Autowired
    SysLogService sysLogService;

    /**
     * 监听消费并处理用户登陆成功后的消息
     * @param dto UserLoginDto
     */
    @RabbitListener(queues = "dev.login.queue")
    @RabbitHandler
    public void consumeMsg(@Payload UserLoginDto dto) {
        try {
            log.info("系统日志记录-消费者-监听消费用户登录成功后的消息-内容: {}", dto);

            //调用日志记录服务-用于记录用户登录成功后将相关登陆信息记入数据库
            sysLogService.recordLog(dto);
        } catch (Exception e) {
            log.error("系统日志记录-消费者-监听消费用户登录成功后的消息-发生异常: {}", dto, e.fillInStackTrace());
        }
    }
}
